use crate::Vec2;

#[derive(Copy, Clone)]
pub struct Rect {
    pub v0: Vec2,
    pub v1: Vec2
}

impl Rect {
    pub fn new(left: f32, right: f32, top: f32, bottom: f32) -> Self {
        Self {
            v0: Vec2(left, bottom),
            v1: Vec2(right, top)
        }
    }

    pub fn from_extent(x: f32, y: f32, w: f32, h: f32) -> Self {
        let hw = w / 2.0;
        let hh = h / 2.0;
        Self {
            v0: Vec2(x - hw, y - hh),
            v1: Vec2(x + hw, y + hh),
        }
    }

    pub fn from_diagonal(p1: Vec2, p2: Vec2) -> Self {
        Self {
            v0: p1,
            v1: p2
        }
    }

    pub fn move_x(&mut self, x: f32) {
        self.v0.0 += x;
        self.v1.0 += x;
    }

    pub fn move_y(&mut self, y: f32) {
        self.v0.1 += y;
        self.v1.1 += y;
    }

    pub fn includes(&self, p: Vec2) -> bool {
        self.includes_xy(p.0, p.1)
    }

    pub fn includes_xy(&self, x: f32, y: f32) -> bool {
        x > self.v0.0 && x < self.v1.0 && y > self.v0.1 && y < self.v1.1
    }

    pub fn overlaps(&self, other: Rect) -> bool {
        self.includes_xy(other.v0.0, other.v1.1) ||
        self.includes_xy(other.v0.0, other.v0.1) ||
        self.includes_xy(other.v1.0, other.v1.1) ||
        self.includes_xy(other.v1.0, other.v0.1)
    }
}